package com.cy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cy.common.entity.Result;
import com.cy.pojo.Admin;
import com.cy.pojo.vo.LoginVo;
import com.cy.pojo.vo.SearchVo;
import com.cy.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author 残影
 * @description 管理员表
 * @date 2022-04-22
 */
@RestController
@RequestMapping("/admin")
@Api(tags = "管理员表模块")
public class AdminController {
    @Autowired
    private AdminService adminService;

    /**
     * 根据ID查询管理员表
     */
    @GetMapping("{id}")
    @ApiOperation("根据ID查询管理员表")
    public Result findById(@PathVariable Integer id) {
        Admin admin = adminService.getById(id);
        if (admin != null) {
            return Result.ok("查询管理员表成功", admin);
        } else {
            return Result.failed(201, "没有该管理员表");
        }
    }

    /**
     * 分页查询管理员表
     */
    @ApiOperation("分页查询管理员表")
    @GetMapping("/list")
    public Result list(SearchVo searchVo) {
        //分页构造器
        Page<Admin> buildPage = new Page<>(searchVo.getPage(), searchVo.getLimit());
        //条件构造器
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(searchVo.getName())) {
            queryWrapper.like("username", searchVo.getName());
        }
        Date startTime = searchVo.getStartTime();
        Date endTime = searchVo.getEndTime();
        if (ObjectUtils.isNotEmpty(startTime) && ObjectUtils.isNotEmpty(endTime)) {
            queryWrapper.between("create_time", startTime, endTime);
        }
        IPage<Admin> pageList = adminService.page(buildPage, queryWrapper);
        return Result.ok("查询成功", pageList.getRecords()).put("total", pageList.getTotal());
    }

    @PostMapping("login")
    @ApiOperation("login")
    public Result login(@RequestBody LoginVo loginVo) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("username", loginVo.getUsername());
        wrapper.eq("password", loginVo.getPassword());
        Admin admin = adminService.getOne(wrapper);
        if (admin == null) {
            return Result.failed(4001, "用户名或密码错误");
        }
        return Result.ok("登录成功");
    }

    /**
     * 保存管理员表
     */
    @PostMapping
    @ApiOperation("保存管理员表")
    public Result save(@RequestBody Admin admin) {
        admin.setCreateTime(new Date());
        admin.setLoginTime(new Date());
        admin.setPermission("aud");
        boolean b = adminService.save(admin);
        if (b) {
            return Result.ok("保存用户管理员表成功", admin);
        } else {
            return Result.failed("保存管理员表失败");
        }
    }

    /**
     * 修改管理员表
     */
    @PutMapping
    @ApiOperation("修改管理员表")
    public Result update(@RequestBody Admin admin) {
        boolean b = adminService.saveOrUpdate(admin);
        if (b) {
            return Result.ok("修改管理员表成功", admin);
        } else {
            return Result.failed("修改管理员表失败");
        }
    }

    /**
     * 删除管理员表
     */
    @DeleteMapping("{id}")
    @ApiOperation("删除管理员表")
    public Result delete(@PathVariable Long id) {
        boolean b = adminService.removeById(id);
        if (b) {
            return Result.ok("删除管理员表成功");
        } else {
            return Result.failed("删除管理员表失败");
        }
    }
}
